home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 14467 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.4 KB

  1. Path: rover.ucs.ualberta.ca!alberta!wong
  2. From: wong@cs.ualberta.ca (Warren Wong)
  3. Newsgroups: comp.lang.c++,de.comp.lang.c++,ualberta.cmput.201
  4. Subject: Re: Another function pointer from a class question?!
  5. Date: 31 Mar 1996 20:35:57 GMT
  6. Organization: Computing Science, U of Alberta, Edmonton, Canada
  7. Message-ID: <4jmqbd$lrk@scapa.cs.ualberta.ca>
  8. References: <4jls2p$bac@pulp.ucs.ualberta.ca>
  9. NNTP-Posting-Host: sawnlk.cs.ualberta.ca
  10.  
  11. ryangall@gpu.srv.ualberta.ca (Ryan Gallagher) writes:
  12.  
  13. >well....its just that I get a linker error when I try and run it when 
  14. >using
  15. >the print function. Since I want to be able to use any structure for
  16. >the list, I will have to supply some sort of printing function to the 
  17. >class,
  18. >otherwise, it will not nessesarily know what to do when printing each 
  19. >member.
  20. >Anyways, this is my first attempt at a template, and derived class....so
  21. >bear with me if I've made any really stupid mistakes.
  22.  
  23. [some stuff]
  24.  
  25. >template<class T>
  26. >// I used to have this next line, as the delaration but it didnt work,
  27. >//class dl_list : public list<T>{
  28. >// it kept saying that I couldnt use a abstract class directly.
  29.  
  30. >class dl_list{
  31. >   struct node{
  32. >         T data;
  33. >         node *next;
  34. >         node *prev;
  35. >         node(T d,node *n,node *p) { data = d; next = n; prev =p;}
  36. >   };
  37.  
  38. >   node *S;
  39. >   node *F;
  40. >   node *C;
  41.  
  42. >public:
  43.  
  44. >   dl_list();
  45. >  ~dl_list();
  46.  
  47. >   int  Add(T);
  48. >   T Remove();
  49. >   T POPfirst();
  50. >   T POPlast();
  51. >   int  empty() { return (F==S); }
  52. >   void print(void (*)(T));
  53.  
  54. >};
  55.  
  56. >/***********************************************************************
  57. >*
  58. >*  print: prints off the data in the list.
  59. >*
  60. >************************************************************************/
  61.  
  62. >template<class T> void list<T>::print(void (*pfunc)(T))
  63. > {
  64. >  node *X=S;
  65.  
  66. >   cout << endl << endl <<endl;
  67. >   cout << "THE LIST" << endl << "--------" << endl;
  68.  
  69.  
  70. >     if(X!=F)
  71.  
  72. >     do
  73. >     {
  74. >        X=X->next;
  75. >        (*pfunc)(X->data);
  76. >     }
  77. >     while( X != F && X);
  78.  
  79. >  return 0;
  80.  
  81. > }
  82.  
  83. >//*************************************************************
  84.  
  85. >void printint(int d)
  86. >{
  87. > printf("( %i )\n",d);
  88. >}
  89.  
  90. It looks like you forgot to write the template function for the dl_list
  91. print function to actually call the function pointer.  You did it for
  92. the list, but since you're not deriving the dl_list from the list (I
  93. think you commented it out) you're not calling that functions.  You just
  94. have to make a template for the print function.
  95.  
  96. -WW
  97.  
  98. --
  99. Warren Wong
  100. wong@cs.ualberta.ca
  101.  
  102.